package com.firefightingiot.firefightingiot.support.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/***
 * 处理流的问题 解决传递JSON时只能获取一次的问题
 * 
 * @author
 *
 */
@WebFilter(filterName="bodyFilter",urlPatterns="/*")
public class BodyFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		if (request.getContentType() != null && request.getContentType().contains("json")) {
			HttpServletRequest httpServletRequest = (HttpServletRequest) request;
			RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest);
			chain.doFilter(requestWrapper, response);
		} else {
			chain.doFilter(request, response);
		}
	}

	@Override
	public void destroy() {

	}

}
